﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using TestMySqlConnetPoll;
using System.Threading;

namespace View.LK
{
    public partial class HistoryLK : Form
    {
        DataTable BindDt = new DataTable();     //绑定到view的data   
        DataSet ddds = new DataSet();           //需要用定时器来播放的dataset集合
        Dictionary<DateTime, DataSet> AllStatueInfo = new Dictionary<DateTime, DataSet>();  //状态信息集合
        string LKtype = "";     //当前选择的菜单的列控类型名称
        string SelectMenu = "";     //当前选择的 菜单
        string LinkMenu = "";       //当前选择的二级菜单
        string txbox1value = "";        //时长选择框值
        string comboBox1value = "";     //时长单位选择框
        int speed = 1;          //播放速率
        bool yjpt = false;      //硬件平台等有设备列表类型
        int playNo = 0;         //当前播放到的dataset中datatable的下标值.
        bool FitFlag = false;       //是否填充了表格
        public HistoryLK()
        {
            InitializeComponent();
            this.dateTimePicker1.MaxDate = DateTime.Now;
            this.dateTimePicker1.MinDate = DateTime.Now.AddMonths(-3);
            this.dateTimePicker1.Value = DateTime.Now.AddMinutes(-30);
            SetSelectValue(true);
            DataTable dt = new DataTable();
            c_LK.InitHostHardwarePlatform(ref dt);
            BindDataTable(dt);
            SelectMenu = "硬件平台";
            LKtype = "列控中心主机硬件平台";
            Text = "列控回放" + "---" + LKtype;
            speed = 1;
            btn_start.Enabled = false;
            btn_stop.Enabled = false;
            btn_speed.Enabled = false;
            btn_dnSpeed.Enabled = false;
        }

        private void FillGridView(DataTable dt)
        {
            //BindDt.Rows.Clear();
            if (BindDt.Columns.Count != dt.Columns.Count) return;
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                for (int j = 0; j < dt.Columns.Count;j++ )
                {
                    BindDt.Rows[i][j] = dt.Rows[i][j];
                }
            }
            if (!yjpt || (!FitFlag))
            {
                FitFlag = true;
                this.gridView1.BestFitColumns();
            }
        }
        private void BindDataTable(DataTable dtt)
        {
            BindDt = dtt;
            this.gridView1.Columns.Clear();
            this.gridControl1.DataSource = BindDt;
            this.gridView1.BestFitColumns();
        }

        private void InitTractBar(int min,int max)
        {
            trackBar1.SetRange(min,max);
            trackBar1.Value = 0;
            trackBar1.TickFrequency = (max - min);
            trackBar1.SmallChange = 5;
            trackBar1.LargeChange = 10;
        }

        private void SetSelectValue(bool f)
        {
            timer1.Stop();
            if(f)
            {
                yjpt = true;
                btn_start.Enabled = false;
                btn_speed.Enabled = false;
                btn_stop.Enabled = false;
                btn_dnSpeed.Enabled = false;
                label_bv.Text = "×1";
                speed = 1;
                label_time.Text = "";
                if (comboBox1value != "分钟")
                {
                    comboBox1.Text = "分钟";
                    comboBox1value = "分钟";
                    txbox1value = "30";
                    textBox1.Text = "30";
                    dateTimePicker1.Text = DateTime.Now.AddMinutes(-30).ToString();
                }
            }
            else
            {                
                yjpt = false;
                btn_start.Enabled = false;
                btn_speed.Enabled = false;
                btn_stop.Enabled = false;
                btn_dnSpeed.Enabled = false;
                label_bv.Text = "";
                label_time.Text = "";
                if (comboBox1value == "分钟")
                {
                    comboBox1.Text = "天";
                    comboBox1value = "天";
                    txbox1value = "1";
                    textBox1.Text = "1";
                    dateTimePicker1.Text = DateTime.Now.Date.ToString();
                }
            }
        }

        private void navBarControl1_GroupExpanded(object sender, DevExpress.XtraNavBar.NavBarGroupEventArgs e)
        {
            string s = e.Group.Caption;
            if (s == SelectMenu) return;
            DataTable ddt = new DataTable();
            DataSet dss = new DataSet();
            SelectMenu = s;
            LinkMenu = "";
            InitTractBar(0, 10);
            if (s == "硬件平台")
            {
                LKtype = "列控中心主机硬件平台";
                SetSelectValue(true);
                c_LK.InitHostHardwarePlatform(ref ddt);                
            }
            else if (s == "联锁接口进路信息")
            {
                LKtype = "列控中心与联锁接口进路信息";
                SetSelectValue(false);
                c_LK.InitLianSuoJieKouRouteInfo(ref ddt);                
            }
            else if (s == "联锁改方命令信息")
            {
                LKtype = "列控中心与联锁改方命令信息";
                SetSelectValue(false);
                c_LK.InitLianSuoGaiFangCommandInfo(ref ddt);
            }
            else if (s == "联锁改方命令回执信息")
            {
                LKtype = "列控中心与联锁改方回执信息";
                SetSelectValue(false);
                c_LK.InitLianSuoGaiFangCommandRet(ref ddt);
            }
            else if (s == "站间改方命令信息")
            {
                LKtype = "列控中心站间改方命令信息";
                SetSelectValue(false);
                c_LK.InitZhanJianGaiFangCommandInfo(ref ddt);
            }
            else if (s == "站间改方回执信息")
            {
                LKtype = "列控中心站间改方回执信息";
                SetSelectValue(false);
                c_LK.InitZhanJianGaiFangCommandRet(ref ddt);
            }
            else if (s == "站间边界信息")
            {
                LKtype = "列控中心站间边界信息";
                SetSelectValue(false);
                c_LK.InitZhanJianBoundaryInfo(ref ddt);
            }
            else if (s == "区间方向口信息")
            {
                LKtype = "发送给联锁的无配线站区间方向口信息";
                SetSelectValue(false);
                c_LK.InitSendToLianSuoInfo(ref ddt);
            }
            else if (s == "临时限速状态")
            {
                LKtype = "列控中心临时限速状态";
                SetSelectValue(false);
                c_LK.InitTemporaryLimitSpeedState(ref ddt);
            }
            else if (s == "区段编码信息")
            {
                LKtype = "轨道区段编码信息";
                SetSelectValue(true);
                c_LK.InitSectionEncodeInfo(ref ddt);
            }
            else if (s == "应答器报文编码信息")
            {
                LKtype = "应答器报文编码信息";
                SetSelectValue(false);
                c_LK.InitTransponderEncodeInfo(ref ddt);
            }
            else if (s == "状态信息")
            {
                LKtype = "列控状态信息";
                SetSelectValue(true);
                c_LK.InitStatueInfo(ref dss);
                ddt = dss.Tables["PIO采集继电器"];
                LinkMenu = "PIO采集继电器";
            }
            BindDataTable(ddt);
            this.Text = "列控信息回放"+"---" + s +"-"+LinkMenu;
        }

        private void textBox1_KeyDown(object sender, KeyEventArgs e)
        {
           
        }

        private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!(Char.IsNumber(e.KeyChar)) && e.KeyChar != (char)13 && e.KeyChar != (char)8)
            {
                e.Handled = true;
            }       
        }

        private void navBarControl1_Click(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (SelectMenu == "") return;
            DataTable dddt = new DataTable();
            LK_Table lktb = new LK_Table();
            lktb.InitLKTable();
            int stime =MySql.DateTimeToTime_t(DateTime.Parse(dateTimePicker1.Text));
            int etime = stime;

            DateTime s = new DateTime(dateTimePicker1.Value.Year, dateTimePicker1.Value.Month, dateTimePicker1.Value.Day, dateTimePicker1.Value.Hour, dateTimePicker1.Value.Minute, dateTimePicker1.Value.Second);
            
            if(comboBox1value == "天")
               etime = MySql.DateTimeToTime_t(DateTime.Parse(dateTimePicker1.Text).AddDays(int.Parse(txbox1value)));
            else if(comboBox1value == "小时")
                etime = MySql.DateTimeToTime_t(DateTime.Parse(dateTimePicker1.Text).AddHours(int.Parse(txbox1value)));
            else
                etime = MySql.DateTimeToTime_t(DateTime.Parse(dateTimePicker1.Text).AddMinutes(int.Parse(txbox1value)));
            lktb.ReadValueList(stime, etime, LKtype);
            while(!lktb.ReadyDataTable)
            {
                if (lktb.ErrorFlag != 0) break;
                else
                    Thread.Sleep(30);
            }
            if (SelectMenu == "硬件平台")
            {                
                ddds = lktb.HostHardwarePlatformSet;
                if (ddds.Tables.Count >0)
                {
                    dddt = lktb.HostHardwarePlatformSet.Tables[0];
                    label_time.Text = lktb.HostHardwarePlatformSet.Tables[0].Rows[0]["A机状态"].ToString();
                    InitTractBar(0, ddds.Tables.Count);
                }
                else
                {
                    label_time.Text = "";
                }
            }
            else if (SelectMenu == "联锁接口进路信息") dddt = lktb.LianSuoJieKouRouteInfo;
            else if (SelectMenu == "联锁改方命令信息") dddt = lktb.LianSuoGaiFangCommandInfo;
            else if (SelectMenu == "联锁改方命令回执信息") dddt = lktb.LianSuoGaiFangCommandRet;
            else if (SelectMenu == "站间改方命令信息") dddt = lktb.ZhanJianGaiFangCommandInfo;
            else if (SelectMenu == "站间改方回执信息") dddt = lktb.ZhanJianGaiFangCommandRet;
            else if (SelectMenu == "站间边界信息") dddt = lktb.ZhanJianBoundaryInfo;
            else if (SelectMenu == "区间方向口信息") dddt = lktb.SendToLianSuoInfo;
            else if (SelectMenu == "临时限速状态") dddt = lktb.TemporaryLimitSpeedState;
            else if (SelectMenu == "区段编码信息")
            {
                ddds = lktb.SectionEncodeInfo;
                if (ddds.Tables.Count>0)
                {
                    dddt = lktb.SectionEncodeInfo.Tables[0];
                    label_time.Text = dddt.TableName.ToString();
                    InitTractBar(0, ddds.Tables.Count);
                }
                else
                {
                    label_time.Text = "";
                }
            }
            else if (SelectMenu == "应答器报文编码信息") dddt = lktb.TransponderEncodeInfo;
            else if (SelectMenu == "状态信息")
            {
                AllStatueInfo = lktb.AllStatueInfo;
                if(AllStatueInfo.Count>0)
                {
                    foreach (DateTime key in AllStatueInfo.Keys)
                    {
                        dddt = AllStatueInfo[key].Tables["PIO采集继电器"];
                        label_time.Text = key.ToString();
                        break;
                    }
                    InitTractBar(0, AllStatueInfo.Count);
                }
            }
            playNo = 0;
            if(dddt.Rows.Count>0)
            {
                if(yjpt)
                {
                    FitFlag = false;
                    btn_start.Enabled = true;
                    btn_speed.Enabled = false;
                    btn_stop.Enabled = false;
                    btn_dnSpeed.Enabled = false;
                }
                FillGridView(dddt);
                
            }
        }

        private void btn_start_Click(object sender, EventArgs e)
        {
            btn_start.Enabled = false;
            btn_stop.Enabled = true;
            btn_speed.Enabled = true;            
            timer1.Start();
        }

        private void btn_stop_Click(object sender, EventArgs e)
        {
            btn_start.Enabled = true;
            btn_stop.Enabled = false;
            btn_speed.Enabled = false;
            btn_dnSpeed.Enabled = false;
            timer1.Stop();
        }

        private void btn_speed_Click(object sender, EventArgs e)
        {
            if (speed < 4) speed++;
            else btn_speed.Enabled = false;
            label_bv.Text = "×"+speed.ToString();
            timer1.Interval = 1000 / speed;
            btn_dnSpeed.Enabled = true;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (!yjpt) return;
            if(LKtype!= "列控状态信息")
            {
                if (playNo < (ddds.Tables.Count - 1))
                {
                    playNo++;
                    if(trackBar1.Value<trackBar1.Maximum)trackBar1.Value++;  //控制进度条
                    FillGridView(ddds.Tables[playNo]);
                    if (LKtype == "列控中心主机硬件平台")
                    {
                        label_time.Text = ddds.Tables[playNo].Rows[0]["A机状态"].ToString();
                    }
                    else if (LKtype == "轨道区段编码信息")
                    {
                        label_time.Text = ddds.Tables[playNo].TableName.ToString();
                    }
                }
                else timer1.Stop();
            }
            else
            {
                if (playNo < (AllStatueInfo.Count - 1))
                {
                    playNo++;
                    trackBar1.Value++;  //控制进度条
                    List<DateTime> keys = AllStatueInfo.Keys.ToList();
                    if(AllStatueInfo[keys[playNo]].Tables.Contains(LinkMenu))
                    {
                        FillGridView(AllStatueInfo[keys[playNo]].Tables[LinkMenu]);
                    }
                    label_time.Text = keys[playNo].ToString();
                }
                else timer1.Stop();
            }            
        }

        private void btn_dnSpeed_Click(object sender, EventArgs e)
        {
            if (speed > 1) speed--;
            else btn_dnSpeed.Enabled = false;
            label_bv.Text = "×" + speed.ToString();
            timer1.Interval = 1000 / speed;
            btn_speed.Enabled = true;
        }

        private void navBarControl1_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            string s = e.Link.Caption;
            if (s == LinkMenu) return;
            LinkMenu = s;
            if (e.Link.Group.Caption == "状态信息")
            {
                DataSet dss = new DataSet();
                c_LK.InitStatueInfo(ref dss);

                BindDataTable(dss.Tables[LinkMenu]);

                timer1.Stop();
                speed = 1;
                label_bv.Text = "×" + speed.ToString();
                timer1.Interval = 1000 / speed;
                foreach(DateTime d in AllStatueInfo.Keys)
                {
                    if(AllStatueInfo[d].Tables.Contains(LinkMenu))
                    {
                        InitTractBar(0, AllStatueInfo.Count);
                        label_time.Text = d.ToString();
                        BindDataTable(AllStatueInfo[d].Tables[LinkMenu]);
                        break;
                    }
                }
                Text = "列控回放" + "---" + SelectMenu + "-" + LinkMenu;
            }
        }

        private void trackBar1_Scroll(object sender, EventArgs e)
        {
            int d = trackBar1.Value;
            playNo = d;
        }
    }
}
